//
//  UIViewController+TYAlertController_PACK.m
//  dyss_v2
//
//  Created by LXF on 16/1/30.
//  Copyright © 2016年 First Facility. All rights reserved.
//

#import "UIViewController+ShowAlertView_PACK.h"

#import "TYAlertController.h"
#import "TYAlertController+BlurEffects.h"
#import "XXAlertView.h"
#import "XXAlertAction.h"

#import "NSMutableSet+XXWeakMutableSet.h"

#define LocalString(key) NSLocalizedString(key, @"")

#import <objc/runtime.h>

@implementation UIViewController (ShowAlertView_PACK)

-(void)dismissXXAlertView{
    TYAlertController *aler = [self alertController];
    [aler dismissViewControllerAnimated:YES];
    self.alertController = nil;
}

-(TYAlertController *)alertController{
    WeakRefNode<TYAlertController *> *node = objc_getAssociatedObject(self, @selector(alertController));
    if (node.isNil) {
        [self setAlertController:nil];
    }
    return node.value;
}
-(void)setAlertController:(TYAlertController *)alertController{
    if (alertController == nil) {
        objc_setAssociatedObject(self, @selector(alertController), alertController, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        return;
    }
    WeakRefNode<TYAlertController *> *node = [WeakRefNode node:alertController];
    objc_setAssociatedObject(self, @selector(alertController), node, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


-(NSArray *)showAlertViewWithTitle:(NSString *)title message:(NSString *)message handler:(alertActionBlock)handler{
    return [self showAlertViewWithTitle:title message:message cancle:LocalString(@"取消") destructive:LocalString(@"确定") handler:handler];
}
-(NSArray *)showAlertViewNoBEWithTitle:(NSString *)title message:(NSString *)message handler:(alertActionBlock)handler{
    return [self showAlertViewNoBEWithTitle:title message:message cancle:LocalString(@"取消") destructive:LocalString(@"确定") handler:handler];
}

-(NSArray *)showAlertViewNoBEWithTitle:(NSString *)title message:(NSString *)message cancle:(NSString *)cancleString destructive:(NSString *)destructiveString handler:(alertActionBlock)handler{
    return [self showAlertViewWithTitle:title message:message cancle:cancleString default:nil destructive:destructiveString openBlurEffect:NO handler:handler];
}

-(NSArray *)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancle:(NSString *)cancleString destructive:(NSString *)destructiveString handler:(alertActionBlock)handler{
    return [self showAlertViewWithTitle:title message:message cancle:cancleString default:nil destructive:destructiveString openBlurEffect:YES handler:handler];
}

-(NSArray *)showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancle:(NSString *)cancleString default:(NSString *)defaultString destructive:(NSString *)destructiveString openBlurEffect:(BOOL)openBE handler:(alertActionBlock)handler{
    XXAlertView *alertView = [XXAlertView alertViewWithTitle:title message:message];
    alertView.buttonCancleBgColor = [UIColor clearColor];
//    typeof(alertView) weakAlertView = weakAlertView;
    if (cancleString) {
        [alertView addAction:[XXAlertAction actionWithTitle:cancleString style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
            handler(kXXAlertActionTypeCancle,(XXAlertAction *)action);
        }]];
    }
    
    if (defaultString) {
        [alertView addAction:[XXAlertAction actionWithTitle:defaultString style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
            handler(kXXAlertActionTypeDefault,(XXAlertAction *)action);
        }]];
        
    }
    
    if (destructiveString) {
        [alertView addAction:[XXAlertAction actionWithTitle:destructiveString style:TYAlertActionStyleCancle handler:^(TYAlertAction *action) {
            handler(kXXAlertActionTypeDestructive,(XXAlertAction *)action);
        }]];
    }
    
    TYAlertController *alertController = [TYAlertController alertControllerWithAlertView:alertView preferredStyle:TYAlertControllerStyleAlert transitionAnimation:TYAlertTransitionAnimationFade];
    __weak typeof(self) weakSelf = self;
    __weak typeof(alertController) weakAlertController = alertController;
    [alertController setDismissComplete:^{
        if (weakSelf.alertController == weakAlertController) {
            weakSelf.alertController = nil;
        }
    }];

    printOK(@"------");
    if (self.alertController) {
        [self.alertController dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    
    self.alertController = alertController;
    
    [self presentViewController:alertController animated:YES completion:nil];
    
    return [alertView valueForKey:@"actions"];
}

@end
